define([ "app", "jquery", "md5", "ecb", 
         "directives/DatetimePicker",
         "services/UserService",
         "resources/OrgResource",
		 "resources/UserResource",
		 "resources/DoubleDetailResource",
		 "services/MessageBox",
		 "directives/common/OrgUserPicker",
		 "directives/common/OrgTreeView",
		 "directives/authority/EditDauthority",
		 "services/TipsService"
		],
		function(app) {

	var deps = [ "$scope", "$state", "$window",  "$location",
			"$stateParams", "MenuService", "UserService","OrgResource", "UserResource","DoubleDetailResource","MessageBox","TipsService"];

	function controller($scope, $state, $window, $location, $stateParams, MenuService, UserService,OrgResource, UserResource,DoubleDetailResource,MessageBox,TipsService) {
		// 通过go传递参数-主键ID 
		var keyId=$stateParams.keyId;
		$scope.user ={};
		$scope.user = UserService.getCurrentUser();
		$scope.selectedUser = {}; // 点击后保存的单个用户
		$scope.selectedUserList = {}; // 点击后保存的用户列表
		$scope.isSelectAll = false;
		
		if(keyId==null || keyId==""){
			keyId =$scope.user.currentCompanyCode;
		}
		$scope.companyCode =keyId;
		$scope.currentPage = 1;
        $scope.pages = [];
        $scope.pageSize = 10;
		//取全部的组织
        OrgResource.getOrgList(keyId).success(function(resp){
        	$scope.allOrgList = resp;
        });
        $scope.treeParam = {orgType:$stateParams.orgType,companyCode:keyId};
        
        // 树节点点击事件
        $scope.orgClick =function (){
        	//获取选中组织
        	var orgId =$scope.selectedOrg.orgId;
        	var searchKey =$scope.searchKey;
        	DoubleDetailResource.queryDoubleAuthorityUserList(orgId,$scope.currentPage,$scope.pageSize,keyId,searchKey).success(function(data){
				$scope.data = data.userList;
				if(data.count % $scope.pageSize == 0){
         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
        		}else{
        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
        		}
            });
        };
        
        
        // 选择所有用户
		$scope.toggleAll = function() {
			if ($scope.isSelectAll) {
				$scope.selectedUser = {}; // 点击后保存的单个用户
				$scope.selectedUserList = {}; // 点击后保存的用户列表
				$scope.isSelectAll = false;
			} else {
				for (var i = 0; i < $scope.data.length; i++) {
					$scope.selectedUserList[$scope.data[i].userId] = $scope.data[i];
				}
				$scope.isSelectAll = true;
			}
		};
		// 删除选择权限-批量删除
		$scope.deleteAuthority =function (){
			var userIds=convertMapToArray($scope.selectedUserList)
			if(userIds.length>0){
				DoubleDetailResource.deleteUsersAuthority(userIds,$scope.companyCode,'doubleAuthority').success(function(data){
					// 置空
					$scope.selectedUserList = {};
	        		var searchKey =$scope.searchKey;
	        		$scope.currentPage = 1;
	        		queryData(searchKey);
	        		$scope.isSelectAll = false;
	            });
			}
		}
		
		// 选择一个用户
		$scope.toggleUser = function(record) {
			$scope.selectedUser = record;
			if($scope.selectedUserList[record.userId]) {
				delete $scope.selectedUserList[record.userId];
			} else {
				$scope.selectedUserList[record.userId] = record;
			}
		};
		
		
        /**
         * 删除用户双人通话权限
         */
        $scope.deleteUserAuthority=function (userId){
        	var userIds=[];
			userIds.push(userId)
        	DoubleDetailResource.deleteUsersAuthority(userIds,$scope.companyCode,'doubleAuthority').success(function(data){
        		var searchKey =$scope.searchKey;
        		// 置空
				$scope.selectedUserList = {};
        		queryData(searchKey);
            });
        };
        
        /**
         * 获取有权限的用户信息
         */
        function queryData(searchKey){
        	var orgId =$scope.selectedOrg.orgId;
        	DoubleDetailResource.queryDoubleAuthorityUserList(orgId,$scope.currentPage,$scope.pageSize,keyId,searchKey).success(function(data){
				$scope.data = data.userList;
				if(data.count % $scope.pageSize == 0){
         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
        		}else{
        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
        		}
            });
        }
        
        /**
    	 * 对象转列表 .
    	 */
    	function convertMapToArray(map) {
    		var array = [];
    		for(var i in map) {
    			array.push(map[i].userId);
    		}
    		return array;
    	}
    	
        $scope.search =function (){
        	var searchKey =$scope.searchKey;
        	$scope.currentPage = 1;
        	queryData(searchKey);
        }
        
        /**
         * 跳转到指定的页面
         */
        $scope.gotoPage = function (currentNum) {
			
			if($scope.currentPage != currentNum){
				 var searchKey =$scope.searchKey;
				 $scope.currentPage = currentNum;
		         queryData(searchKey);
			}
        };
        
        /**
         * 下一页
         */
        $scope.nextPage = function () {
        	if($scope.currentPage<$scope.totalPage){
        		var searchKey =$scope.searchKey;
        		$scope.currentPage++;
        		queryData(searchKey);
        	}
        };

        /**
         * 上一页
         */
        $scope.upPage = function () {
        	if($scope.currentPage>1){
        		var searchKey =$scope.searchKey;
        		$scope.currentPage--;
        		queryData(searchKey);
        	}	
        };
        
        /**
         * 监听刷新广播
         */
        
        $scope.$on('dAuthorityEditBroadCast', function($event,data) {
        	var searchKey =$scope.searchKey;
        	queryData(searchKey);
			$event.preventDefault();
        });
        
        /**
         * 新增用户双人通话权限回调
         */
        $scope.addUserAuthority = function(data){
        	var userIds=[] ;
        	angular.forEach(data.list, function(item) {
        		userIds.push(item.userId);
        	});
        	if(userIds.length == 0){
        		return;
        	}
        	DoubleDetailResource.grantDoubleAuthority(userIds, 0,keyId,data.time,data.chooseType).success(function(operatorFlag){
        		TipsService.show("<span class='glyphicon glyphicon-ok'></span>&nbsp;保存成功", 500);
        		//$scope.tableParams.reload();
        		var searchKey =$scope.searchKey;
        		queryData(searchKey);
        	});
        };
    };
	
	controller.$inject = deps;
	return app.lazy.controller("DoubleDetailController", controller);
});
